<!-- 我的订单 -->
<template>
	<view class="my-order page-background">
		<van-tabs color="#2E87FF" :sticky="true" @change="onTabChange" :active="tabsActive" swipeable>
			<van-tab title="全部">
				<scroll-view :scroll-top="scrollTop" :lower-threshold="10" :style="'height:'+(windowHeight-50)+'px'" scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower"
				@scroll="scroll">
				<xjx-nodata v-if="all.list.length==0&&all.loaded"></xjx-nodata>
					<xjx-item
						v-for="(item,index) in all.list"
						:key="index"
						@click="toDetail(item.id)"
						:title="item.clubName" 
						:status="item.statusName" 
						:iconUrl="item.goodsUrl" 
						:name="item.goodsName" 
						:price="item.amount|floorTwo" 
						:arrowShow="true"
						:clubId="item.clubId"
						:type="item.type"  
						:actType="item.actType" 
						:placeName="item.placeName"
						:actLevelStart="item.actLevelStart"
						:actLevelEnd="item.actLevelEnd"
						:girlLevelStart="item.girlLevelStart"
						:girlLevelEnd="item.girlLevelEnd"
						:actStartDate="item.actStartDate"
						:actStartTime="item.actStartTime"
						:actEndTime="item.actEndTime"
						:coachIcon="item.coachIcon"
						:tagsArr="matchTagsArr(item)">
						<view class="btn-row" slot="btn-row">
							<view v-if="item.status=='0'" @click.stop="toCancel(item.id)" class="btn btn-cancel">
								取消订单
							</view>
							<view v-if="item.status=='0'" @click.stop="toPay(item.id)" class="btn btn-sure">
								立即付款
							</view>
							<view v-if="item.status=='6'&&item.type=='2'&&item.isRefund=='0'&&item.isPraised=='0'" @click.stop="toEvaluate(item)" class="btn btn-sure">
								评价
							</view>
							<view v-if="item.status=='6'&&item.type=='2'&&item.isRefund=='0'&&item.isPraised=='1'" @click.stop="toEvaluateDetail(item)" class="btn btn-cancel">
								评价详情
							</view>
						</view>
					</xjx-item>
					<view v-if="all.list.length>0" class="bottom-text">
						<van-loading v-if="!all.allPagesLoaded" type="spinner" :size="24"/>
						<text v-else>我也是有底线的～</text>
					</view>
				</scroll-view>
			</van-tab>
			<van-tab title="待付款">
				<scroll-view :scroll-top="scrollTop" :style="'height:'+(windowHeight-50)+'px'" scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower"
				@scroll="scroll">
					<xjx-nodata v-if="noPay.list.length==0&&noPay.loaded"></xjx-nodata>
					<xjx-item
						v-for="(item,index) in noPay.list"
						:key="index"
						@click="toDetail(item.id)"
						:title="item.clubName" 
						:status="item.statusName" 
						:iconUrl="item.goodsUrl" 
						:name="item.goodsName" 
						:price="item.amount|floorTwo" 
						:arrowShow="true"
						:clubId="item.clubId"
						:type="item.type"
						:actType="item.actType" 
						:placeName="item.placeName"
						:actLevelStart="item.actLevelStart"
						:actLevelEnd="item.actLevelEnd"
						:girlLevelStart="item.girlLevelStart"
						:girlLevelEnd="item.girlLevelEnd"
						:actStartDate="item.actStartDate"
						:actStartTime="item.actStartTime"
						:actEndTime="item.actEndTime"
						:coachIcon="item.coachIcon"
						:tagsArr="matchTagsArr(item)">
						<view class="btn-row" slot="btn-row">
							<view v-if="item.status=='0'" @click.stop="toCancel(item.id)" class="btn btn-cancel">
								取消订单
							</view>
							<view v-if="item.status=='0'" @click.stop="toPay(item.id)" class="btn btn-sure">
								立即付款
							</view>
							<view v-if="item.status=='6'&&item.type=='2'&&item.isRefund=='0'&&item.isPraised=='0'" @click.stop="toEvaluate(item)" class="btn btn-sure">
								评价
							</view>
							<view v-if="item.status=='6'&&item.type=='2'&&item.isRefund=='0'&&item.isPraised=='1'" @click.stop="toEvaluateDetail(item)" class="btn btn-cancel">
								评价详情
							</view>
						</view>
					</xjx-item>
					<view v-if="noPay.list.length>0" class="bottom-text">
						<van-loading v-if="!noPay.allPagesLoaded" type="spinner" :size="24"/>
						<text v-else>我也是有底线的～</text>
					</view>
				</scroll-view>
			</van-tab>
			<van-tab title="待使用">
				<scroll-view :scroll-top="scrollTop" :style="'height:'+(windowHeight-50)+'px'" scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower"
				@scroll="scroll">
					<xjx-nodata v-if="paied.list.length==0&&paied.loaded"></xjx-nodata>
					<xjx-item
						v-for="(item,index) in paied.list"
						:key="index"
						@click="toDetail(item.id)"
						:title="item.clubName" 
						:status="item.statusName" 
						:iconUrl="item.goodsUrl" 
						:name="item.goodsName" 
						:price="item.amount|floorTwo" 
						:arrowShow="true"
						:clubId="item.clubId"
						:type="item.type"
						:actType="item.actType" 
						:placeName="item.placeName"
						:actLevelStart="item.actLevelStart"
						:actLevelEnd="item.actLevelEnd"
						:girlLevelStart="item.girlLevelStart"
						:girlLevelEnd="item.girlLevelEnd"
						:actStartDate="item.actStartDate"
						:actStartTime="item.actStartTime"
						:actEndTime="item.actEndTime"
						:coachIcon="item.coachIcon"
						:tagsArr="matchTagsArr(item)">
						<view class="btn-row" slot="btn-row">
							<view v-if="item.status=='0'" @click.stop="toCancel(item.id)" class="btn btn-cancel">
								取消订单
							</view>
							<view v-if="item.status=='0'" @click.stop="toPay(item.id)" class="btn btn-sure">
								立即付款
							</view>
							<view v-if="item.status=='6'&&item.type=='2'&&item.isRefund=='0'&&item.isPraised=='0'" @click.stop="toEvaluate(item)" class="btn btn-sure">
								评价
							</view>
							<view v-if="item.status=='6'&&item.type=='2'&&item.isRefund=='0'&&item.isPraised=='1'" @click.stop="toEvaluateDetail(item)" class="btn btn-cancel">
								评价详情
							</view>
						</view>
					</xjx-item>
					<view v-if="paied.list.length>0" class="bottom-text">
						<van-loading v-if="!paied.allPagesLoaded" type="spinner" :size="24"/>
						<text v-else>我也是有底线的～</text>
					</view>
				</scroll-view>
			</van-tab>
			<van-tab title="待评价">
				<scroll-view :scroll-top="scrollTop" :style="'height:'+(windowHeight-50)+'px'" scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower"
				@scroll="scroll">
					<xjx-nodata v-if="noPraise.list.length==0&&noPraise.loaded"></xjx-nodata>
					<xjx-item
						v-for="(item,index) in noPraise.list"
						:key="index"
						@click="toDetail(item.id)"
						:title="item.clubName" 
						:status="item.statusName" 
						:iconUrl="item.goodsUrl" 
						:name="item.goodsName" 
						:price="item.amount|floorTwo" 
						:arrowShow="true"
						:clubId="item.clubId"
						:type="item.type"
						:actType="item.actType" 
						:placeName="item.placeName"
						:actLevelStart="item.actLevelStart"
						:actLevelEnd="item.actLevelEnd"
						:girlLevelStart="item.girlLevelStart"
						:girlLevelEnd="item.girlLevelEnd"
						:actStartDate="item.actStartDate"
						:actStartTime="item.actStartTime"
						:actEndTime="item.actEndTime"
						:coachIcon="item.coachIcon"
						:tagsArr="matchTagsArr(item)">
						<view class="btn-row" slot="btn-row">
							<view v-if="item.status=='0'" @click.stop="toCancel(item.id)" class="btn btn-cancel">
								取消订单
							</view>
							<view v-if="item.status=='0'" @click.stop="toPay(item.id)" class="btn btn-sure">
								立即付款
							</view>
							<view v-if="item.status=='6'&&item.type=='2'&&item.isRefund=='0'&&item.isPraised=='0'" @click.stop="toEvaluate(item)" class="btn btn-sure">
								评价
							</view>
							<view v-if="item.status=='6'&&item.type=='2'&&item.isRefund=='0'&&item.isPraised=='1'" @click.stop="toEvaluateDetail(item)" class="btn btn-cancel">
								评价详情
							</view>
						</view>
					</xjx-item>
					<view v-if="noPraise.list.length>0" class="bottom-text">
						<van-loading v-if="!noPraise.allPagesLoaded" type="spinner" :size="24"/>
						<text v-else>我也是有底线的～</text>
					</view>
				</scroll-view>
			</van-tab>
			<van-tab title="退款/售后">
				<scroll-view :scroll-top="scrollTop" :style="'height:'+(windowHeight-50)+'px'" scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower"
				@scroll="scroll">
					<xjx-nodata v-if="refund.list.length==0&&refund.loaded"></xjx-nodata>
					<xjx-item
						v-for="(item,index) in refund.list"
						:key="index"
						@click="toRefundDetail(item.id)"
						:title="item.clubName" 
						:status="item.statusValue" 
						:iconUrl="item.goodsUrl" 
						:name="item.goodsName" 
						:price="item.orderAmount|floorTwo" 
						:arrowShow="true"
						:clubId="item.clubId"
						:type="item.type"
						:actType="item.actType" 
						:placeName="item.placeName?item.placeName:item.actPlaceName"
						:actLevelStart="item.levelStart"
						:actLevelEnd="item.levelEnd"
						:girlLevelStart="item.girlLevelStart"
						:girlLevelEnd="item.girlLevelEnd"
						:actStartDate="item.actStartDate"
						:actStartTime="item.actStartTime"
						:actEndTime="item.actEndTime"
						:coachIcon="item.coachIcon"
						:tagsArr="matchTagsArr(item)">
					</xjx-item>
					<view v-if="refund.list.length>0" class="bottom-text">
						<van-loading v-if="!refund.allPagesLoaded" type="spinner" :size="24"/>
						<text v-else>我也是有底线的～</text>
					</view>
				</scroll-view>
			</van-tab>
		</van-tabs>
		<!-- 弹出框 -->
		<van-dialog id="van-dialog" confirm-button-color="#2E87FF" cancel-button-color="#2E87FF" />
	</view>
</template>

<script>
	import { mapState, mapGetters, mapMutations } from 'vuex'
	import Dialog from '@/wxcomponents/vant/dist/dialog/dialog';
	export default {
		data() {
			return {
				tabsActive: 0,
				findStatus: 0, // 0.全部，1.待付款，2.已付款，3.待评价，4.退款
				findStatusVal: 'all',
				all:{
					list: [],
					loaded: false,
					pageNo: 0
				},
				noPay:{
					list: [],
					loaded: false,
					pageNo: 0
				},
				paied:{
					list: [],
					loaded: false,
					pageNo: 0
				},
				noPraise:{
					list: [],
					loaded: false,
					pageNo: 0
				},
				refund:{
					list: [],
					loaded: false,
					pageNo: 0
				},
				isLoad: true,
				windowHeight: 667,
				defaultPageSize: 20,
				needReload: false
			};
		},
		onLoad(data) {
			let _this = this
			this.tabsActive = Number(data.type) || 0
			if(this.tabsActive == 0){
				this.getList(1,this.defaultPageSize)
			}
			uni.getSystemInfo({
				success(res) {
					_this.windowHeight = res.windowHeight
				}
			})
		},
		onShow(){
			if(this.isLoad){
				this.isLoad = false
			}else{
				// this.getList(1,this.defaultPageSize)
				// 从详情页返回到列表页，刷新当前tab列表，其他列表未加载过的，不需要刷新，加载过的需要刷新
				this.reloadList()
			}
		},
		computed:{
			...mapState(['userInfo','accessToken']),
		},
		methods: {
			scrollTop(){
				
			},
			scroll(){
				
			},
			upper(){
				
			},
			lower(){
				let val = this.matchOrderType(this.findStatus)
				if(!this[val].allPagesLoaded){
					this.getList(this[val].pageNo+1,this.defaultPageSize)
				}
			},
			reloadList(){
				if(this.findStatus==4){
					this.reloadTabList(4)
					if(this.all.loaded){
						this.reloadTabList(0)
					}
				}else{
					this.reloadTabList(this.findStatus)
					if(this.findStatus!=0&&this.all.loaded){
						this.reloadTabList(0)
					}else if(this.findStatus!=1&&this.noPay.loaded){
						this.reloadTabList(1)
					}else if(this.findStatus!=2&&this.paied.loaded){
						this.reloadTabList(2)
					}else if(this.findStatus!=3&&this.noPraise.loaded){
						this.reloadTabList(3)
					}
				}
			},
			async reloadTabList(findStatus){
				let val = this.matchOrderType(findStatus)
				if(findStatus == 4){
					let param = {
						'ACCESS-TOKEN': this.accessToken,
						pageSize: this[val].pageNo * this.defaultPageSize,
						pageNo: 1
					}
					const res = await this.$api.queryOrderRefundList(param)
					if(res && res.code == 200){
						if(!this.refund.loaded){
							this.refund.loaded = true
						}
						if(res.data.list&&res.data.list.length>0){
							this.refund.list = res.data.list
						}
						if(this.refund.list.length == res.data.count && this.refund.pageNo * this.defaultPageSize >= res.data.count){
							this.refund.allPagesLoaded = true
						}
					}
				}else{
					let param = {
						'ACCESS-TOKEN': this.accessToken,
						pageSize: this[val].pageNo * this.defaultPageSize,
						pageNo: 1
					}
					if(findStatus){
						param.findStatus = findStatus
					}
					const res = await this.$api.queryOrderList(param)
					if(res && res.code == 200){
						if(!this[val].loaded){
							this[val].loaded = true
						}
						this[val].list = res.data.list
						if(this[val].list.length == res.data.count && this[val].pageNo * this.defaultPageSize >= res.data.count){
							this[val].allPagesLoaded = true
						}
					}
				}
			},
			getList(pageNo, pageSize){
				if(this.findStatus == 4){
					this.getRefundList(pageNo, pageSize)
				}else{
					this.getOrderList(pageNo, pageSize)
				}
			},
			matchTagsArr(item){
				if(item.type=='1'){
					let arr = []
					if(item.goodsSpec){
						arr.push(item.goodsSpec)
					}
					if(item.actLevelStart&&item.actLevelEnd){
						arr.push('L'+item.actLevelStart+'-L'+item.actLevelEnd)
					}
					return arr
				}else if(item.type=='2'){
					if(item.goodsSpec){
						return [item.goodsSpec]
					}
					return []
				}else{
					return []
				}
			},
			async getOrderList(pageNo, pageSize, status=undefined){
				let param = {
					'ACCESS-TOKEN': this.accessToken,
					pageSize: pageSize,
					pageNo: pageNo
				}
				if(status){
					param.findStatus = this.status
				}else if(this.findStatus){
					param.findStatus = this.findStatus
				}
				let val = this.matchOrderType(param.findStatus)
				const res = await this.$api.queryOrderList(param)
				if(res && res.code == 200){
					if(!this[val].loaded){
						this[val].loaded = true
					}
					if(res.data.list&&res.data.list.length>0){
						this[val].list.push(...res.data.list)
					}
					this[val].pageNo = pageNo
					if(this[val].list.length == res.data.count && this[val].pageNo * this.defaultPageSize >= res.data.count){
						this[val].allPagesLoaded = true
					}
				}
			},
			async getRefundList(pageNo, pageSize){
				let param = {
					'ACCESS-TOKEN': this.accessToken,
					pageSize: pageSize,
					pageNo: pageNo
				}
				const res = await this.$api.queryOrderRefundList(param)
				if(res && res.code == 200){
					if(!this.refund.loaded){
						this.refund.loaded = true
					}
					if(res.data.list&&res.data.list.length>0){
						this.refund.list.push(...res.data.list)
						this.refund.pageNo = pageNo
					}
					if(this.refund.list.length == res.data.count && this.refund.pageNo * this.defaultPageSize >= res.data.count){
						this.refund.allPagesLoaded = true
					}
				}
			},
			onTabChange(e) {
				let index = e.detail.index
				this.findStatus = index
				this.findStatusVal = this.matchOrderType(index)
				if(!this[this.findStatusVal].loaded){
					this.getList(1,this.defaultPageSize)
				}
			},
			toDetail(id){
				uni.navigateTo({
					url: `/personalCenter/myOrder/detail/detail?id=${id}`
				})
			},
			toRefundDetail(id) {
				uni.navigateTo({
					url: `/personalCenter/myOrder/refundDetail/refundDetail?type=1&id=${id}`
				})
			},
			toCancel(id) {
				Dialog.confirm({
					title: '确认取消此订单？',
					cancelButtonText: '取消',
					width: '325px',
					messageAlign: 'left',
					customStyle: 'border-radius:7.5px;',
					className: 'my-dialog-confirm'
				})
				.then(() => {
					this.makeSureCancel(id)
				})
				.catch(() => {})
			},
			async makeSureCancel(id){
				const param = {
					'ACCESS-TOKEN': this.accessToken
				}
				const res = await this.$api.cancelOrder(param, id)
				if(res && res.code == 200){
					if(this.all.loaded){
						this.reloadTabList(0)
					}
					if(this.noPay.loaded){
						this.reloadTabList(1)
					}
				}
				uni.showToast({
					title: res.message,
					icon: 'none'
				})
			},
			matchOrderType(i) {
				switch(i){
					case 0:
						return 'all'
					case 1:
						return 'noPay'
					case 2:
						return 'paied'
					case 3:
						return 'noPraise'
					case 4:
						return 'refund'
					default:
						return 'all'
				}
			},
			async toPay(id) {
				uni.navigateTo({
				    url:'/mainProcess/confirmOrder/payType?id='+id
				})
			},
			toEvaluate(item) {
				if(item.classType==2){
					uni.navigateTo({
						url: `/mainProcess/evaluate/index/index?orderId=${item.id}&state=2&type=1`
					})
				}else{
					uni.navigateTo({
						url: `/mainProcess/evaluate/index/index?type=2&state=2&orderId=${item.id}`
					})
				}
				
			},
			toEvaluateDetail(item) {
				uni.navigateTo({
					url: `/mainProcess/evaluate/index/index?orderId=${item.id}&state=1&type=${item.classType==2?'1':'2'}`
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.my-order {
		min-height: 100vh;
		overflow: hidden;
		background: #FFFFFF;
		.order-list{
			padding-bottom: 15rpx;
		}
		.bottom-text{
			text-align: center;
			line-height: 40px;
			height: 80rpx;
			font-size: 24rpx;
			color: #999;
		}
		.btn-row {
			display: flex;
			justify-content: flex-end;
			.btn {
				width: 151rpx;
				padding: 5rpx;
				border-radius: 5px;
				text-align: center;
				margin-left: 20rpx;
				&.btn-sure {
					font-size: 26rpx;
					background: #6BA9FE;
					color: #FFFFFF;
				}
				&.btn-cancel {
					font-size: 26rpx;
					background: #BABABA;
					color: #FFFFFF;
				}
			}

		}
	}
</style>

<style lang="scss">
	page{
		height: 100%;
	}
	.van-tabs__nav {
		height: 50px;
		line-height: 50px;

		.van-ellipsis {
			line-height: 50px;
			padding: 0!important;
		}

		.van-tab--active {
			color: $public-color-blue1;
		}
	}

	.van-tabs__wrap {
		height: 50px !important;
	}

	.van-tabs {
		.van-sticky {
			z-index: 2 !important;
		}
	}

	.van-tab__pane--active {
		border-top: 1rpx solid #E9E9E9 !important;
	}
</style>
